/**
 * Created by meiguizhinian on 2018/6/20
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

ivy.AniPlayer = cc.Node.extend({
	_className:"ivy.AniPlayer",
	/**
	 *
	 * argument:
	 *       BaseAniPlayer
	 *       AnimationID
	 *       int32_t classID,         int32_t actionID=0, int32_t sequenceID=0, bool selfUpdate=true, AniPlayState playState=ivy.AniPlayState.eCircle, AniPlayType playType=ivy.AniPlayType.eNormal
	 *       std::string aniFileName, int32_t actionID=0, int32_t sequenceID=0, bool selfUpdate=true, AniPlayState playState=ivy.AniPlayState.eCircle, AniPlayType playType=ivy.AniPlayType.eNormal
	 * */
	ctor : function () {
		this._super();
		let player = null;
		if (arguments.length>0) {
			let ani = arguments[0];
			if (ani instanceof ivy.AnimationID) {
				player = ivy.BaseAniPlayer.create(ani.classID, ani.actionID, ani.sequenceID, ani.selfUpdate, ani.playState, ani.playType);
			} else if (ani instanceof ivy.BaseAniPlayer) {
				player = ani;
			} else {
				let actionID=0, sequenceID=0, selfUpdate=true, playState=ivy.AniPlayState.eCircle, playType=ivy.AniPlayType.eNormal;
				switch (arguments.length){
					case 6: {actionID=arguments[1];sequenceID=arguments[2];selfUpdate=arguments[3];playState=arguments[4];playType=arguments[5];break;}
					case 5: {actionID=arguments[1];sequenceID=arguments[2];selfUpdate=arguments[3];playState=arguments[4];break;}
					case 4: {actionID=arguments[1];sequenceID=arguments[2];selfUpdate=arguments[3];break;}
					case 3: {actionID=arguments[1];sequenceID=arguments[2];break;}
					case 2: {actionID=arguments[1];break;}
				}
				player = ivy.BaseAniPlayer.create(ani, actionID, sequenceID, selfUpdate, playState, playType);
			}

			this._aniPlayer = player;
			this.addChild(this._aniPlayer);
		}
	},

	getPlayerType : function () {
		if (this._aniPlayer) {
			return this._aniPlayer.getPlayerType();
		}
		return ivy.AniPlayerType.eNone;
	},

	resetAnimation : function (animationID) {
		if (this._aniPlayer) {
			let willChangeType = ivy.BaseAniPlayer.getPlayerType(animationID);
			if (willChangeType !== this._aniPlayer.getPlayerType()) {
				let ptr = this._aniPlayer;
				ptr.setVisible(false);
				ivy.Tools.pushFunctionToCocosThread(function(){
				    ptr.removeFromParent();
				});
				this._aniPlayer = ivy.BaseAniPlayer.create(animationID);
				this.addChild(this._aniPlayer);
			} else {
				this._aniPlayer.resetAnimation(animationID);
			}
		} else {
			this._aniPlayer = ivy.BaseAniPlayer.create(animationID);
			this.addChild(this._aniPlayer);
		}
	},

	reset : function() {
		if (arguments.length===0) {
			if (this._aniPlayer)
				this._aniPlayer.reset();
		}  else {
			let animationID = arguments[0];
			this.resetAnimation(animationID);
		}
	},

	setPlayState : function (aniPlayState, autoResetSeqID) {
		if (this._aniPlayer) {
			if (autoResetSeqID===null) {
				autoResetSeqID = true;
			}
			this._aniPlayer.setPlayState(aniPlayState, autoResetSeqID);
		}
	},

	setPlayType : function (aniPlayType, autoResetSeqID) {
		if (this._aniPlayer) {
			if (autoResetSeqID===null) {
				autoResetSeqID = true;
			}
			this._aniPlayer.setPlayType(aniPlayType, autoResetSeqID);
		}
	},

	autoSetSequenceID : function () {
		if (this._aniPlayer) {
			this._aniPlayer.autoSetSequenceID();
		}
	},

	setActionID : function (actionID, resetSeqence) {
		if (this._aniPlayer) {
			if (resetSeqence===null) {
				resetSeqence = true;
			}
			this._aniPlayer.setActionID(actionID, resetSeqence);
		}
	},

	getActionID : function () {
		if (this._aniPlayer) {
			return this._aniPlayer.getActionID();
		}
		return 0;
	},

	getActionCount : function () {
		if (this._aniPlayer) {
			return this._aniPlayer.getActionCount();
		}
		return 0;
	},

	getSequenceCount : function (actionID) {
		if (this._aniPlayer) {
			return this._aniPlayer.getSequenceCount(actionID);
		}
		return 0;
	},

	setSequenceID : function (seqID) {
		if (this._aniPlayer) {
			this._aniPlayer.setSequenceID(seqID);
		}
	},

	getSuitChangeID : function (imgID) {
		if (this._aniPlayer) {
			return this._aniPlayer.getSuitChangeID(imgID);
		}
		return 0;
	},

	setSuitChange : function (imgID, suitID) {
		if (this._aniPlayer) {
			this._aniPlayer.setSuitChange(imgID, suitID);
		}
	},

	clearSuitChangeData : function () {
		if (this._aniPlayer) {
			this._aniPlayer.clearSuitChangeData();
		}
	},

	setSelfUpdate : function (up) {
		if (this._aniPlayer) {
			this._aniPlayer.setSelfUpdate(up);
		}
	},

	isKeyFrameDataNeedToDeal : function () {
		if (this._aniPlayer) {
			return this._aniPlayer.isKeyFrameDataNeedToDeal();
		}
		return false;
	},

	getKeyFrameBaseData : function (keyFrameBaseType, actionID, seqID) {
		if (this._aniPlayer)
			return this._aniPlayer.getKeyFrameBaseData(keyFrameBaseType, actionID, seqID);
		return 0;
	},

    getKeyFrameCustomData : function (type, actionID, seqID) {
	    if (this._aniPlayer)
		    return this._aniPlayer.getKeyFrameCustomData(type, actionID, seqID);
        return 0;
    },

    getKeyFrameCustomZoneData : function(type, actionID, seqID) {
		if (this._aniPlayer) {
            return this._aniPlayer.getKeyFrameCustomZoneData(type, actionID, seqID);
        }
        return cc.RectZero();
    },

    isKeyFrameBaseDataExist : function (keyFrameBaseType, actionID, seqID) {
		if (this._aniPlayer) {
            return this._aniPlayer.isKeyFrameBaseDataExist(keyFrameBaseType, actionID, seqID);
        }
        return false;
    },

    isKeyFrameCustomDataExist : function (type, actionID, seqID) {
		if (this._aniPlayer) {
            return this._aniPlayer.isKeyFrameCustomDataExist(type, actionID, seqID);
        }
        return false;
    },

    isKeyFrameCustomZoneDataExist : function (type, actionID, seqID) {
		if (this._aniPlayer) {
            return this._aniPlayer.isKeyFrameCustomZoneDataExist(type, actionID, seqID);
        }
        return false;
    },

	getAttackBox : function() {
	    if (this._aniPlayer)
			return this._aniPlayer.getAttackBox();
		return cc.RectZero();
    },

    getCollisionBox : function () {
		if (this._aniPlayer)
            return this._aniPlayer.getCollisionBox();
	    return cc.RectZero();
    },

    setShowColAtkBoxRect : function(show) {
	if (this._aniPlayer)
		return this._aniPlayer.setShowColAtkBoxRect(show);
    },

	//////////////////////////////////////////////////////////////////////////
	getPlayState : function () {
        if (this._aniPlayer) {
            return this._aniPlayer.getPlayState();
        }
        return cc.AniPlayState.eEnumCount;
    },

    getPlayType : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.getPlayType();
        }
		return cc.AniPlayState.eEnumCount;
    },

    getActionIDBak : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.getActionIDBak();
        }
        return 0;
    },

	setAnimationID : function (aniID) {
	    if (this._aniPlayer) {
		    this._aniPlayer.setAnimationID(aniID);
        }
    },

    getAnimationID : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.getAnimationID();
        }
        return -1;
    },

    getSequenceID : function () {
        if (this._aniPlayer) {
            return this._aniPlayer.getSequenceID();
        }
        return 0;
    },

    isFlippedX : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.isFlippedX();
        }
        return false;
    },

    setFlippedX : function (filp) {
		if (this._aniPlayer) {
			this._aniPlayer.setFlippedX(filp);
	    }
    },

    getSelfUpdate : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.getSelfUpdate();
        }
        return false;
    },

    setAnimationData : function (aniData) {
	    if (this._aniPlayer) {
		    this._aniPlayer.setAnimationData(aniData);
        }
    },

    getAnimationData : function () {
		if(this._aniPlayer) {
            return this._aniPlayer.getAnimationData();
        }
        return null;
    },

    getPlayerChildrenVec : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.getPlayerChildrenVec();
        }
        return [];
    },

    isPlayOver : function () {
		if (this._aniPlayer) {
            return this._aniPlayer.isPlayOver();
        }
        return false;
    },

    getSequenceDurationValue : function () {
		if (this._aniPlayer) {
		    return this._aniPlayer.getSequenceDurationValue();
        }
        return 0;
    },

    getMotionValue : function() {
        if (this._aniPlayer) {
            return this._aniPlayer.getMotionValue();
        }
        return 1.0;
    },

    setMotionValue : function(mV) {
	    if (mV===null) {
	        mV = 1.0;
        }
        if (this._aniPlayer) {
	        this._aniPlayer.setMotionValue(mV);
        }
    },

    setExDuration : function(d) {
        if (this._aniPlayer) {
	        this._aniPlayer.setExDuration(d);
        }
    },

    getExDuration : function () {
        if (this._aniPlayer) {
            return this._aniPlayer.getExDuration();
        }
        return 0;
    },

    setPlayOverFunction : function(func) {
        if (this._aniPlayer) {
	        this._aniPlayer.setPlayOverFunction(func);
        }
    },

    //对当前动画播放器设置混合绘制 所有子节点将使用当前设置进行混合
    setCustomBlendFunc : function (blendFunc) {
        if (this._aniPlayer) {
	        this._aniPlayer.setCustomBlendFunc(blendFunc);
        }
    },

    getCustomBlendFunc : function() {
        if(this._aniPlayer) {
            return this._aniPlayer.getCustomBlendFunc();
        }
        return null;
    },

    getBaseAniPlayer : function() {
		return this._aniPlayer;
    },
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	update : function (dt) {
		this._super.update();
		if (this._aniPlayer) {
			this._aniPlayer.update(dt);
		}
	},

	clone : function () {
		if (this._aniPlayer) {
			let newAniPlayer = ivy.AniPlayer.create(this._aniPlayer.clone());
			if (newAniPlayer) {
				newAniPlayer.setScale(this.getScale());
				newAniPlayer.setRotation(this.getRotation());
			}
			return newAniPlayer;
		}
		return null;
	},

	setColor : function (color) {
		this._super.setColor(color);
		if (this._aniPlayer) {
			this._aniPlayer.setColor(color);
		}
	},

	getColor : function () {
		if (this._aniPlayer) {
			return this._aniPlayer.getColor();
		}
		return this._super.getColor();
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	_aniPlayer : null,
});

ivy.AniPlayer.create = function (ani, actionID=0, sequenceID=0, selfUpdate=true, playState=ivy.AniPlayState.eCircle, playType=ivy.AniPlayType.eNormal) {
	return new ivy.AniPlayer(ani, actionID, sequenceID, selfUpdate, playState, playType);
};